﻿/**************************************************************************
            　　　　　　　　　　　　　JS翻页
            　作者：刘光
            　日期：2008-10-7
            　说明：
            　　　保证showCount*width/pageMoveCount和showCount*width/pageMoveCount
            　结果为整数。
            　
***************************************************************************/   
        
//移动对象
function MoveObject(srcObj,time,length,count)
{
    var index = 0;
    var timeID = setInterval(function(){
        if(index<count)
        {
            srcObj.style.position="absolute";
            var left = srcObj.offsetLeft+length;
            srcObj.style.left = left+"px";
            index++;
        }
        else
        {
            clearInterval(timeID);
        }
    },time);
}       
var lgod_MoveParm =
    {
        "obj":null,        //要移动的对象
        "index":0,         //当前显示页左边第一项的索引（从１开始）
        "width":0,         //每一个项的宽度
        "sumCount":0,      //总共显示的项数                      
        "showCount":0,     //每页显示的项数
        "minIndex":0,      //最小的索引值，为１
        "maxIndex":0,      //最大的索引值
        "pageMoveCount":0, //翻页时移动的次数
        "itemMoveCount":0  //滚动一个项时移动的次数
    };
    
//初始化翻页参数
function InitMoveParm(moveObj,index,width,sumCount,showCount,pageMoveCount,itemMoveCount)
{
    lgod_MoveParm.obj=moveObj;
    lgod_MoveParm.index=index;
    lgod_MoveParm.width=width;
    lgod_MoveParm.sumCount=sumCount;
    lgod_MoveParm.showCount=showCount;
    lgod_MoveParm.minIndex=1;
    lgod_MoveParm.maxIndex=sumCount-showCount+1; 
    lgod_MoveParm.pageMoveCount=pageMoveCount;
    lgod_MoveParm.itemMoveCount=itemMoveCount;
}
//首页
function FirstPage()
{
    if(lgod_MoveParm.index>lgod_MoveParm.minIndex)
    {
        var tmp = lgod_MoveParm.index-lgod_MoveParm.minIndex;
        MoveObject(lgod_MoveParm.obj,10,tmp*lgod_MoveParm.width,1)
        //MoveObject(obj,100,100,index-minIndex);若想要滚动效果，需要计算时间和次数
        lgod_MoveParm.index=lgod_MoveParm.minIndex;
    }
}
//尾页
function LastPage()
{
    if(lgod_MoveParm.index<lgod_MoveParm.maxIndex)
    {
        var tmp = lgod_MoveParm.maxIndex-lgod_MoveParm.index;
        MoveObject(lgod_MoveParm.obj,10,-tmp*lgod_MoveParm.width,1)
        //MoveObject(obj,1000,-100,maxIndex-index);
        lgod_MoveParm.index=lgod_MoveParm.maxIndex;
    }
}
//上一页
function PrePage()
{
    if(lgod_MoveParm.index>lgod_MoveParm.showCount)
    {
        MoveObject(lgod_MoveParm.obj,1,(lgod_MoveParm.showCount*lgod_MoveParm.width)/lgod_MoveParm.pageMoveCount,lgod_MoveParm.pageMoveCount);
        lgod_MoveParm.index=lgod_MoveParm.index-lgod_MoveParm.showCount;
    }
    else
    {
        FirstPage();
    }
}
//下一页
function NextPage()
{
    if(lgod_MoveParm.index<lgod_MoveParm.maxIndex-lgod_MoveParm.showCount)
    {
        MoveObject(lgod_MoveParm.obj,1,-(lgod_MoveParm.showCount*lgod_MoveParm.width)/lgod_MoveParm.pageMoveCount,lgod_MoveParm.pageMoveCount);
        lgod_MoveParm.index=lgod_MoveParm.index+lgod_MoveParm.showCount;
    }
    else
    {
        LastPage();
    }
}
//上一项
function Pre()
{
    if(lgod_MoveParm.index>lgod_MoveParm.minIndex)
    {
        MoveObject(lgod_MoveParm.obj,1,lgod_MoveParm.width/lgod_MoveParm.itemMoveCount,lgod_MoveParm.itemMoveCount);
        lgod_MoveParm.index=lgod_MoveParm.index-1;
    }
}
//下一项
function Next()
{
    if(lgod_MoveParm.index<lgod_MoveParm.maxIndex)
    {
        MoveObject(lgod_MoveParm.obj,1,-lgod_MoveParm.width/lgod_MoveParm.itemMoveCount,lgod_MoveParm.itemMoveCount);
        lgod_MoveParm.index=lgod_MoveParm.index+1;
    }
}
//跳转到指定页
function GoPage(pageIndex)
{
    var tmpIndex=lgod_MoveParm.showCount*(pageIndex-1)+1;
    if((tmpIndex+lgod_MoveParm.showCount-1)>lgod_MoveParm.sumCount)
    {
        LastPage();
    }
    else
    {
        var tmp = tmpIndex-lgod_MoveParm.index;
        MoveObject(lgod_MoveParm.obj,10,-tmp*lgod_MoveParm.width,1);
        lgod_MoveParm.index=tmpIndex;
    }
}

